package com.winit.openapi.e2e.controller;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.spi.context.CommandContext;
import com.winit.erp.spi.bpartner.vo.Bpartner;
import com.winit.oms.spi.e2e.E2EFirstLegShipmentFeeService;
import com.winit.oms.spi.e2e.command.E2EFeeCalculateCommand;
import com.winit.oms.spi.e2e.vo.E2EPscFeeVo;
import com.winit.oms.spi.e2e.vo.ItemInfo;
import com.winit.openapi.constants.ApiConstant;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.ValidateUtil;

/**
 * E2E头程费用相关接口
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * hui.li2 	1.0  		2017年4月18日 	Created
 *
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping(value = "/e2e/firstfee")
public class E2EFirstLegShipmentFeeController extends BaseController {

    @Resource
    private E2EFirstLegShipmentFeeService e2eFirstLegShipmentFeeService;

    /**
     * 头程费用计算
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/calculateFee")
    public String calculateFee(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        E2EFeeCalculateCommand command = new E2EFeeCalculateCommand();
        command.setCtx(CommandContext.getContext());
        List<E2EPscFeeVo> result = null;
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;

            ValidateUtil.validNotNull(json, "inspWhCode", "InspectionWarehouse");
            ValidateUtil.validNotNull(json, "dispWhCode", "DispatchWarehouse");
            ValidateUtil.validNotNull(json, "countryCode", "Country");
            ValidateUtil.validNotNull(json, "postCode", "Post");
            ValidateUtil.validNotNull(json, "currency", "Currency");
            ValidateUtil.validNotNull(json, "itemLength", "ItemLength");
            ValidateUtil.validNotNull(json, "itemWidth", "ItemWidth");
            ValidateUtil.validNotNull(json, "itemHeight", "ItemHeight");
            ValidateUtil.validNotNull(json, "itemWeight", "ItemWeight");
            ValidateUtil.validNotNull(json, "weight", "Weight");
            ValidateUtil.validNotNull(json, "volume", "Volume");
            ValidateUtil.validNotNull(json, "importCustomsMode", "ImportCustomsMode");
            ValidateUtil.validNotNull(json, "exportCustomsMode", "ExportCustomsMode");
            ValidateUtil.validNotNull(json, "pickupType", "PickupType");

            command.setInspWhCode(json.getString("inspWhCode"));
            command.setDispWhCode(json.getString("dispWhCode"));
            command.setCountryCode(json.getString("countryCode"));
            command.setPostCode(json.getString("postCode"));
            command.setCurrency(json.getString("currency"));
            command.setIsBattery(json.getString("isBattery"));
            command.setDoiNum(json.getInteger("doiNum"));
            command.setWeight(json.getBigDecimal("weight"));
            command.setVolume(json.getBigDecimal("volume"));
            command.setImportCustomsMode(json.getString("importCustomsMode"));
            command.setExportCustomsMode(json.getString("exportCustomsMode"));
            command.setPickupType(json.getString("pickupType"));

            ItemInfo itemInfo = new ItemInfo();
            itemInfo.setLength(json.getBigDecimal("itemLength"));
            itemInfo.setWidth(json.getBigDecimal("itemWidth"));
            itemInfo.setHeight(json.getBigDecimal("itemHeight"));
            itemInfo.setWeight(json.getBigDecimal("itemWeight"));
            double d = json.getBigDecimal("weight")
                .divide(json.getBigDecimal("itemWeight"))
                .setScale(2, BigDecimal.ROUND_HALF_UP)
                .doubleValue();
            itemInfo.setQty((int) Math.ceil(d));
            itemInfo.setVolume(json.getBigDecimal("itemLength")
                .multiply(json.getBigDecimal("itemWidth").multiply(json.getBigDecimal("itemHeight")
                    .divide(BigDecimal.valueOf(1000000), 9, RoundingMode.HALF_UP))));

            command.setItemInfo(itemInfo);

            Bpartner bpartner = CommandContext.getContext().get(ApiConstant.API_BPARTNER);
            if (bpartner != null) {
                command.setCustomerCode(bpartner.getValue());
            } else {
                command.setCustomerCode(CommandContext.getContext().getUserId());
            }

            // 调用费用预估接口
            result = e2eFirstLegShipmentFeeService.calculateFee(command);

        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        responseMsg.setData(result);
        responseMsg.setMsg("SUCCESS");
        return SUCCESS;
    }
}
